package cn.btks.backstagemanage.division.controller;

import cn.btks.backstagemanage.division.dto.DivisionDto;
import cn.btks.backstagemanage.division.pojo.City;
import cn.btks.backstagemanage.division.pojo.County;
import cn.btks.backstagemanage.division.service.CityService;
import cn.btks.backstagemanage.unit.pojo.Unit;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.commonserver.util.JwtUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author lijun
 * @Date 2021/1/25 11:09
 */
@RestController
@RequestMapping("api/v1.0/unit")
@Api(tags = "行政区划模块")
@Validated
public class CityController {

    @Autowired
    CityService cityService;

    @GetMapping("/find/city/findById/{id}")
    @ApiOperation("根据ID查询师信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "师ID", required = true, dataType = "long"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findById(@PathVariable("id") Long id) {
        City city = cityService.findById(id);
        return city != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), city) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/find/city/findAll")
    @ApiOperation("查询所有师区划信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAll() {
        List<City> cityList = cityService.findAll();
        return cityList != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), cityList) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/find/city/findAllCityAndCounty")
    @ApiOperation("查询所有师及其下的团场信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAllCityAndCounty() {
        List<DivisionDto> divisionDtoList = cityService.findAllCityAndCounty();
        return divisionDtoList != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), divisionDtoList) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/find/city/findByCityName/{cityName}")
    @ApiOperation("根据名称查询师信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "cityName", value = "师名称", required = true, dataType = "string"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findByCityName(@PathVariable("cityName") String cityName) {
        City city = cityService.findByCityName(cityName);
        return city != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), city) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/find/city/findByCountyName/{countyName}")
    @ApiOperation("根据团场名称查询其所属师信息(代管团场则查询代管师)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "countyName", value = "团场名称", required = true, dataType = "string"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findByCountyName(@PathVariable("countyName") String countyName) {
        City city = cityService.findByCountyName(countyName);
        return city != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), city) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/find/city/findAllByDivision")
    @ApiOperation("根据用户确定是师还是兵团来获取区划信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAllByDivision(HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Optional<Claims> optional = JwtUtil.getClaimsFromToken(token, JwtUtil.jwtsSecret);
        if(optional.isPresent()){
            Claims claims = optional.get();
            Object obj = claims.get("unit");
            Unit unit = JSONArray.parseObject(JSON.toJSONString(obj), Unit.class);
            if(unit != null){
                if(unit.getCityCode().equals("65")){
                    List<City> cityList = cityService.findAllByIsShow(1);
                    return cityList != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                            StateInfo.FIND_OK.getMsg(), cityList) : new ResultState(StateInfo.FIND_NULL.getCode(),
                            StateInfo.FIND_NULL.getMsg());
                }else {
                    List<City> cityList = new ArrayList<>();
                    City city = cityService.findById(unit.getCityId());
                    cityList.add(city);
                    return cityList != null && cityList.size() > 0 ? new ResultState(StateInfo.FIND_OK.getCode(),
                            StateInfo.FIND_OK.getMsg(), cityList) : new ResultState(StateInfo.FIND_NULL.getCode(),
                            StateInfo.FIND_NULL.getMsg());
                }
            }
        }
        return new ResultState(StateInfo.TOKEN_ERROR.getCode(), StateInfo.TOKEN_ERROR.getMsg());
    }
}
